package io;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import http.HttpUtil;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: ycy
 * @CreateDate: 2018/10/11 14:52
 */
public class csvDemo {


  public static void main(String[] args) {


    print();
//    print1();

  }

  public static void print1() {
    // 读取csv文件操作(1).withHeader(headersArr)这个方法添加后,遍历会跳过表头 (2)自动trim除去首尾空格
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(new String[]{}).withSkipHeaderRecord().withIgnoreEmptyLines(false).withTrim();
    try {
      String inFilePath = "D:\\Users\\yincy\\Desktop\\交通\\new.csv";
      CSVParser csvReadParser = new CSVParser(new BufferedReader(new InputStreamReader(new FileInputStream(inFilePath), "utf-8")), csvFileFormat);
      Iterator<CSVRecord> iterator = csvReadParser.iterator();
      List<String> line = Lists.newArrayList();
      while (iterator.hasNext()) {
        CSVRecord csvRecord = iterator.next();
        String from = csvRecord.get(0);
        String to = csvRecord.get(1);
        System.out.println(from + "  " + to);
        line.add(from + "到" + to + "的汽车票");
      }
      FileUtils.writeLines(new File("D:\\Users\\yincy\\Desktop\\交通\\newparse.csv"), line);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // 初始化 CSVParser object
//    CSVParser csvReadParser = new CSVParser(br, csvFileFormat);
  }


  public static void print() {
    // 读取csv文件操作(1).withHeader(headersArr)这个方法添加后,遍历会跳过表头 (2)自动trim除去首尾空格
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(new String[]{}).withSkipHeaderRecord().withIgnoreEmptyLines(false).withTrim();
    try {
      String inFilePath = "D:\\Users\\yincy\\Desktop\\百度纠错词\\11577219-3.csv";
      CSVParser csvReadParser = new CSVParser(new BufferedReader(new InputStreamReader(new FileInputStream(inFilePath), "utf-8")), csvFileFormat);
      Iterator<CSVRecord> iterator = csvReadParser.iterator();
      List<String> line = Lists.newArrayList();
      while (iterator.hasNext()) {
        CSVRecord csvRecord = iterator.next();
        String keyWord = csvRecord.get(0);
        if (keyWord.trim().length() > 1 && keyWord.trim().length() < 6) {
          line.add(keyWord.trim());
          System.out.println(keyWord.trim());
        }
      }
      FileUtils.writeLines(new File("D:\\Users\\yincy\\Desktop\\百度纠错词\\11577219-4.csv"), line);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // 初始化 CSVParser object
//    CSVParser csvReadParser = new CSVParser(br, csvFileFormat);
  }

}
